package Projo;

import java.awt.event.*;
import java.sql.*;

import javax.swing.*;

import Database.*;
import Exception.*;
import General.GeneralInformation;
import General.TreatError;

public class Student
{
	private static int play = 1, pause = 2, care = 3, end = 4;
	private int year, time, id;
	private String firstName, lastName, section, group;
	private Connection connection;
	private CallDB callDB;
	private MyTableModel myTableModel;
	private String requestRetrieve;
	private JLabel logoStudent;
	private JProgressBar progressBar;
	private ThreadBar threadBar;
	private JButton buttonStart, buttonPause, buttonStop, buttonStopGeneral;
	
	public Student(Connection connection, int time, int id) throws CheckDataException, LoadDriverException
	{
		this.connection = connection;
		callDB = new CallDB();
		
		this.id = id;
		this.time = time*600; //Transform in time *600 at the end
		
		MyActionListener actionListener = new MyActionListener();
		
		retrieveInfo();
		creationComponent(actionListener);
	}

	public void retrieveInfo() throws CheckDataException
	{
		requestRetrieve = "SELECT * FROM student WHERE id = " + id + ";";
		
		myTableModel = callDB.checkData(this.connection, requestRetrieve);

		this.firstName = myTableModel.getValueAt(0,2).toString();
		this.lastName = myTableModel.getValueAt(0,3).toString();
	}
	
	private class MyActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0)
		{
			try
			{
				if(arg0.getSource() == buttonStart) //If the button start is pressed
				{
					buttonStart.setEnabled(false); //We forbid the access twice in a row
					buttonPause.setEnabled(true); //We authorise to pause
					
					//Refresh the logo
					if(threadBar.getEndFlag() == 1) //If the end is reached
					{	
						buttonStop.setEnabled(false);
						buttonPause.setEnabled(false);
						buttonStart.setEnabled(false);
						setLogoStudent(end); //Refresh red logo
					} 
					else
					{
						if(threadBar.getCareFlag() == 1) //If the middle is over
							setLogoStudent(care); //Refresh orange logo
						else
							setLogoStudent(play); //Refresh green logo
					
						//Process Treatment
						if(threadBar.getPauseFlag() == 1) //If the pause is ON
							threadBar.setPauseFlag(0); //Reset the Flag to OFF
		
						if(threadBar.getStartFlag() == 0) //If the the thread is not running
						{
							threadBar.setStartFlag(1); //Set Start Flag to 1
							threadBar.start(); //Start
							AssessmentMgmt asStartMgmt = new AssessmentMgmt(connection);
							asStartMgmt.startAssessment(GeneralInformation.getIdExaminationLaunch(), id);
						}
					}
				}
				else if(arg0.getSource() == buttonPause)
				{
					buttonPause.setEnabled(false); //We forbid the access twice in a row
					buttonStart.setEnabled(true); //We authorise to continue
					
					if(threadBar.getEndFlag() == 1) //If the end is reached
					{	
						buttonStop.setEnabled(false);
						buttonPause.setEnabled(false);
						buttonStart.setEnabled(false);
						setLogoStudent(end); //Refresh red logo
					}
					else
					{
						threadBar.setPauseFlag(1); //Set the Flag to 1
						setLogoStudent(pause); //Refresh blue logo
					}
				}
				else if(arg0.getSource() == buttonStop)
				{
					buttonStop.setEnabled(false);
					buttonPause.setEnabled(false);
					buttonStart.setEnabled(false);
					threadBar.end();
					setLogoStudent(end); //Refresh red logo	
					
					AssessmentMgmt asStopMgmt = new AssessmentMgmt(connection);
					asStopMgmt.endOfAssessment(GeneralInformation.getIdExaminationLaunch(), id, threadBar.getPauseTimer());
				}
			}
			catch(CheckDataException | InsertionDataException | LoadDriverException error)
			{
				TreatError.showError(error);
			}
		}
	}
	
	public void creationComponent(MyActionListener actionListener)
	{
		logoStudent = new JLabel(new ImageIcon("image/miniStudentStart.png"));
		
		progressBar = new JProgressBar();
		progressBar.setStringPainted(true);
		progressBar.setString(firstName + " " + lastName);
		threadBar = new ThreadBar(this);

		buttonStart = new JButton("Start");
		buttonStart.addActionListener(actionListener);
		buttonStart.setName("Start");
		
		buttonPause = new JButton("Pause");
		buttonPause.addActionListener(actionListener);
		buttonPause.setName("Pause");
		
		buttonStop = new JButton("Stop");
		buttonStop.addActionListener(actionListener);
		buttonStop.setName("Stop");
	}
	
	public void setLogoStudent(int state)
	{
		if(state == play)
			logoStudent.setIcon(new ImageIcon("image/miniStudentStart.png"));
		else if(state == pause)
			logoStudent.setIcon(new ImageIcon("image/miniStudentPause.png"));
		else if(state == care)
			logoStudent.setIcon(new ImageIcon("image/miniStudentCare.png"));
		else if(state == end)
			logoStudent.setIcon(new ImageIcon("image/miniStudentStop.png"));		

		logoStudent.repaint();
	}
	
	//Gettors
	public int getYear(){return year;}
	public String getFirstName(){return firstName;}
	public String getLastName(){return lastName;}
	public String getSection(){return section;}
	public String getGroup(){return group;}
	public JProgressBar getProgressBar(){return progressBar;}
	public JLabel getLogoStudent(){return logoStudent;}
	public JButton getButtonStart(){return buttonStart;}
	public JButton getButtonPause(){return buttonPause;}
	public JButton getButtonStop(){return buttonStop;}
	public int getTime(){return time;}
	public int getID(){return id;}
	public ThreadBar getThread(){return threadBar;}
	public Connection getConnection(){return this.connection;}	// For the thread child
}